/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package de.blitzcoder.collide.gui.fileexplorer;

import de.blitzcoder.collide.engine.FileOpener;
import java.awt.Component;
import java.awt.event.ComponentEvent;
import java.awt.event.ComponentListener;
import java.awt.event.HierarchyEvent;
import java.awt.event.HierarchyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.io.File;
import javax.swing.JLabel;
import javax.swing.JTable;
import javax.swing.ListSelectionModel;
import javax.swing.table.TableCellRenderer;

/**
 *
 * @author blitzcoder
 */
public class FileTable extends JTable {

    public FileTable() {
        super(new FileTableModel());

        setGridColor(getBackground());
        setRowHeight(20);
        setColumnSelectionAllowed(false);
        setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
        setAutoResizeMode(AUTO_RESIZE_OFF);
        getColumnModel().getColumn(0).setPreferredWidth(20); // Icon


        
        setDefaultRenderer(JLabel.class, new TableCellRenderer() {

            
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                if (value instanceof Component) {
                    return (Component) value;
                }
                return new JLabel("Error");
            }
        });

        addMouseListener(new MouseListener() {

            
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 2) {
                    FileOpener.open(getModel().getFiles()[getSelectedRow()]);
                    
                }
            }

            
            public void mousePressed(MouseEvent e) {
            }

            
            public void mouseReleased(MouseEvent e) {
            }

            
            public void mouseEntered(MouseEvent e) {
            }

            
            public void mouseExited(MouseEvent e) {
            }
        });



        addHierarchyListener(new HierarchyListener() {

            
            public void hierarchyChanged(HierarchyEvent e) {
                getParent().addComponentListener(new ComponentListener() {

                    
                    public void componentResized(ComponentEvent e) {
                        getColumnModel().getColumn(1).setPreferredWidth(getParent().getWidth() - 20); // Icon
                    }

                    
                    public void componentMoved(ComponentEvent e) {
                    }

                    
                    public void componentShown(ComponentEvent e) {
                    }

                    
                    public void componentHidden(ComponentEvent e) {
                    }

                });
            }
        });

    }

    public void setDir(File dir) {
        ((FileTableModel) getModel()).setDir(dir);
    }

    
    public FileTableModel getModel() {
        return (FileTableModel) super.getModel();
    }
}
